* Discretion in Clinical Decision Making: Evidence from Bunching
* Claire Boone
* Encuesta Nacional de Salud (ENS) 2016-2017
* https://epi.minsal.cl/encuesta-ens-descargable/

clear

global projdir    "`c(pwd)'"
global raw_data   "${projdir}/raw_data"
global gen_data   "${projdir}/gen_data"
global out     	  "${projdir}/out"


* 2016-2017 dataset 
* Forms: F1 questionnaire and F2 questionnaire and bio measurements 
* with EX1 exams 
import spss using "$raw_data/Base de datos Encuesta Nacional de Salud 2016-2017(ENS).Formulario 1_2_EX.MINSAL_EPI. (2).sav", case(lower)

* cleaning
	ren m2p11a_pas pa_sys
	ren m2p11a_pad pa_dia
	ren edad age
	ren m4p1 weight_kg
	ren imc bmi
	
	gen bmi_cat = 0 if bmi <18.5
	replace bmi_cat = 1 if bmi>=18.5 & bmi <25
	replace bmi_cat = 2 if bmi>=25 & bmi<30
	replace bmi_cat = 3 if bmi>=30
	
	gen diag_htn = 0 if h2 ==3|h2==4
	replace diag_htn=1 if h2==1|h2==2

	gen antihypertensives = 0 
	replace antihypertensives = 1 if m2p1==1
	
	gen ever_smoker=0
	replace ever_smoker=1 if ta2==1
	
	gen current_smoker = 0
	replace current_smoker=1 if ta3==1 | ta3==2
	
	gen diag_dm = 0
	replace diag_dm = 1 if di3==1
	
	ren m4p2 height_cm
	ren m4p3 circumference
	
	gen male = 0
	replace male=1 if sexo==1
	
	gen age_cat = 1 if age>=15 & age <=19
	replace age_cat = 2 if age>=20 & age<=24
	replace age_cat = 3 if age>=25 & age<=29
	replace age_cat = 4 if age>=30 & age<=34
	replace age_cat = 5 if age>=35 & age<=39
	replace age_cat = 6 if age>=40 & age<=44
	replace age_cat = 7 if age>=45 & age<=49
	replace age_cat = 8 if age>=50 & age<=54
	replace age_cat = 9 if age>=55 & age<=59
	replace age_cat = 10 if age>=60 & age<=64
	replace age_cat = 11 if age>=65 & age<=69
	replace age_cat = 12 if age>=70 & age<=74
	replace age_cat = 13 if age>=75 & age<=79
	replace age_cat = 14 if age>=80 & age<=84
	replace age_cat = 15 if age>=85 & age<=89
	replace age_cat = 16 if age>=90 & age<=94
	replace age_cat = 17 if age>=95 & age<=99
	
	* make unique ID is a combination of home and person ID
	gen idpersona_1s = string(idpersona_1)
	gen  idencuestas = string(idencuesta)
	gen id = idencuestas + "-" + idpersona_1s
	drop idencuestas idpersona_1s
	
	
	* create indicator for eligible for PSCV  vs. not
	gen pscv_smoke_55 = 0
	replace pscv_smoke_55 = 1 if current_smoker==1 & age>=55

	ta dis2, m
	ta dis2, nolab
	gen diag_dislip = 0
	replace diag_dislip = 1 if dis2==1|dis2==2
	
	
	replace d1_f1 = . if d1_f1== -9999 | d1_f1 == -8888
	replace d1_f1 = 0 if d1_f1 ==2
	
	replace d3_2 = 0 if d1_f1== . 
	replace d3_2 = 1 if d1_f1== 2 
	
	replace d4 = . if d4== -9999 | d4 == -8888
	replace d4 = 0 if d4 ==2
	
	replace d7 = . if d7== -9999 | d7 == -8888
	replace d7 = 0 if d7 ==2
	
	gen pscv_cvd = 0
	replace pscv_cvd=1 if d1_f1==1 | d3_2==1 | d4==1 | d7==1
	
	
	* any pscv other than hypertension
	gen any_pscv = 0 
	replace any_pscv = . if diag_htn==1 
	
	replace any_pscv= 1 if pscv_cvd==1 & any_pscv!=. 
	replace any_pscv= 1 if diag_dislip==1 & any_pscv!=.
	replace any_pscv= 1 if diag_dm==1 & any_pscv!=.
	replace any_pscv = 1 if pscv_smoke_55==1 & any_pscv!=.
	
	ta any_pscv
	ta any_pscv if diag_htn==1, m // patients with htn are exluded
	
	
	keep id age male pa_sys pa_dia weight_kg bmi bmi_cat diag_htn age_cat ///
		 antihypertensives ever_smoker current_smoker diag_dm height_cm ///
		 circumference colesterol_hdl colesterol_ldl_calculado ///
		 colesterol_no_hdl_calculado colesterol_total colesterol_vldl_calculado ///
		 af1a af1b af1c af2a af2b af2c ///
		 diag_dislip pscv_smoke_55  any_pscv pscv_cvd
	
	keep if pa_sys !=.
		
	export delimited "$gen_data/ens_2016_analysis.csv", replace
